/*
 * Copyright (c) OpenKoala 2011 All Rights Reserved
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.openkoala.koala.dd.event;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.openkoala.koala.dd.pojo.DDTree;

/**
 * 类    名：SerializableUtil.java
 *   
 * 功能描述：数据字典的序列化帮助
 *  
 * 创建日期：2013-2-1下午2:10:24     
 * 
 * 版本信息：
 * 
 * 版权信息：Copyright (c) 2013 Koala All Rights Reserved
 * 
 * 作    者：lingen(lingen.liu@gmail.com)
 * 
 * 修改记录： 
 * 修 改 者    修改日期     文件版本   修改说明	
 */
public class SerializableUtil {
    private static String STATIC_DB;//="~/.koala/static_dict";

    private static String QUERY_DB;// = "~/.koala/query_dict";
    static {
        String usrHome = System.getProperty("user.home");
        usrHome = usrHome.replaceAll("\\\\", "/");
        usrHome = usrHome + "/.koala/";
        if (new File(usrHome).exists() == false)
            new File(usrHome).mkdirs();
        STATIC_DB = usrHome + "static.dict";
        QUERY_DB = usrHome + "query.dict";
    }

    /**
     * 将静态查询的数据字典序列化到文件中
     * @param dicts
     */
    public static void serializableStaticDict(List<DDTree> dicts) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream(STATIC_DB);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(dicts);
            oos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null)
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

    /**
     * 从存储文件中读取数据字典
     * @return
     */
    public static List<DDTree> getStaticDictFromSerializable() {
        FileInputStream fis = null;
        ObjectInputStream oin = null;
        List<DDTree> dicts = new ArrayList<DDTree>();
        try {
            fis = new FileInputStream(STATIC_DB);
            oin = new ObjectInputStream(fis);
            dicts = (List<DDTree>)oin.readObject();
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (oin != null)
                try {
                    oin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        return dicts;
    }

    public static void main(String args[]) {
        System.out.println("HEHE");
    }
}
